<?php

namespace App\HttpController\Admin\Auth;

use App\HttpController\Admin\BaseController;

use App\Logic\Auth\Form\MenuListForm;
use App\Logic\Auth\Form\MenuSaveForm;
use App\Logic\Auth\MenuService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

/**
 * Class Menu
 * @package App\HttpController\Admin\Auth
 * @ApiGroup(groupName="权限-菜单管理")
 * @ApiGroupDescription("权限-菜单管理-Description")
 */
class Menu extends BaseController{

    /**
     * @Api(name="列表",path="/admin/auth/menu/index")
     * @Method(allow={GET})
     * @ApiDescription("列表")
     * @ApiSuccessParam(name="id",description="spu id")
     * @ApiSuccessParam(name="title",description="菜单名称")
     * @ApiSuccessParam(name="identification",description="标识")
     * @ApiSuccessParam(name="类型",description="类型")
     * @ApiSuccessParam(name="sort",description="排序")
     * @ApiSuccessParam(name="child",description="子菜单")
     */
    public function index(){
        $params = $this->request()->getQueryParams();

        $listForm = new MenuListForm($params, $this->request());

        $result = $listForm->lists();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="保存",path="/api/auth/menu/action")
     * @ApiDescription("保存")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="id",description="ID值【更新时必填】",type="integer")
     * @Param(name="pid",description="父级ID",required="",type="integer")
     * @Param(name="identification",description="标识",required="",type="integer")
     * @Param(name="title",description="分类名称",required="",type="string")
     * @Param(name="sort",description="排序",required="",type="integer")
     * @Param(name="state",description="是否显示",required="",type="integer")
     * @Param(name="type",description="类型",required="",type="integer")
     * @ApiSuccessParam(name="id",description="操作id",type="integer")
     */
    public function action(){
        $jsonData = $this->getJson();

        $saveForm = new MenuSaveForm($jsonData);

        $result = $saveForm->saveData();

        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="字典",path="/admin/auth/menu/dict")
     * @ApiDescription("字典")
     * @ApiSuccessParam(name="type_list",description="供应商类型",type="array")
     * @Method(allow={GET})
     */
    public function dict(){
        $result = MenuService::getInstance()->getDict();
        return $this->writeJsonByOutput($result);
    }

    /**
     * @Api(name="删除",path="/api/auth/menu/delete")
     * @ApiDescription("删除记录")
     * @Method(allow={POST})
     * @ApiAuth(name="jwt-token",description="JWT-TOKEN值【放入到header请求】",type="string")
     * @Param(name="selected",description="删除的ID",required="",type="array")
     * @ApiSuccessParam(name="affectedRows",description="影响行数",type="integer")
     */
    public function delete(){
        $selected = $this->getJson('selected');

        $result = MenuService::getInstance()->deleteRecord($selected);

        return $this->writeJsonByOutput($result);
    }


}
